package com.zt.rongda.stock.controller;

import com.zt.servlet.*;
import com.zt.rongda.stock.model.StockInventory;
import com.zt.rongda.stock.service.StockInventoryService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping(value = "/stockInventory")
public class StockInventoryController {

    @Autowired
    private StockInventoryService stockInventoryService;

    @RequestMapping(value = "/query")
    @ResponseBody
    
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(stockInventoryService.getTable(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockInventoryService.add(getStockInventory(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockInventoryService.update(getStockInventory(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockInventoryService.delete(reqBean.getInfo("stockId"));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private StockInventory getStockInventory(ReqBean reqBean) {
        StockInventory stockInventory = new StockInventory();
        stockInventory.setStockId(reqBean.getInfo("stockId"));
        stockInventory.setStockName(reqBean.getInfo("stockName"));
        stockInventory.setTypeId(reqBean.getInfo("typeId"));
        stockInventory.setTypeName(reqBean.getInfo("typeName"));
        stockInventory.setSpec(reqBean.getInfo("spec"));
        stockInventory.setUnit(reqBean.getInfo("unit"));
        stockInventory.setInventoryTime(reqBean.getInfoDate("inventoryTime"));
        stockInventory.setYearNum(reqBean.getInfoInt("yearNum"));
        stockInventory.setMonthIn(reqBean.getInfoInt("monthIn"));
        stockInventory.setMonthOut(reqBean.getInfoInt("monthOut"));
        stockInventory.setMonthNum(reqBean.getInfoInt("monthNum"));
        stockInventory.setMonthNumManual(reqBean.getInfoInt("monthNumManual"));
        stockInventory.setOperator(reqBean.getUserId());
        stockInventory.setRemark(reqBean.getInfo("remark"));
        return stockInventory;
    }

}